<template>
  <el-card>
    <el-row>
      <el-col :span="24">
        <div class="search">
          <el-button size="large" class="btn" type="primary" @click="add()" v-auth="auths.add">
            {{ $t('operation.add') }}
          </el-button>
          <el-tree-select
            v-model="query.typeId"
            :data="types"
            :props="{ label: 'typeName' }"
            :render-after-expand="false"
            node-key="id"
            check-strictly
            check-on-click-node
            auto-expand-parent
            :placeholder="$t('boat.type')"
            size="large"
            class="inpt"
            clearable
          />
          <el-select v-model="query.brandId" size="large" :placeholder="$t('boat.brand')" clearable class="inpt">
            <el-option v-for="item in brands" :key="item.id" :label="item.brandName" :value="item.id"/>
          </el-select>
          <el-input v-model="query.model" size="large" :placeholder="$t('boat.model')" clearable class="inpt"></el-input>
          <el-button size="large" type="primary" @click="search()" v-auth="auths.add">
            {{ $t('operation.search') }}
          </el-button>
        </div>
      </el-col>
    </el-row>
    <el-table border style="margin: 10px 0px" :data="list">
      <el-table-column :label="$t('boat.type')" show-overflow-tooltip prop="model" width="140px"></el-table-column>
      <el-table-column :label="$t('boat.brand')" show-overflow-tooltip prop="typeName" width="140px"></el-table-column>
      <el-table-column :label="$t('boat.model')" show-overflow-tooltip prop="brandName" width="140px"></el-table-column>
      <el-table-column :label="$t('boat.number')" show-overflow-tooltip prop="crew" width="100px"></el-table-column>
      <el-table-column :label="$t('boat.height')" show-overflow-tooltip prop="draft" width="170px">
        <!-- <template #="{ row, $index }">
            <div class="number"><span>{{ row.draftFt }} | {{ row.draftM }}</span></div>
        </template> -->
      </el-table-column>
      <el-table-column :label="$t('boat.length')" show-overflow-tooltip prop="length" width="170px">
        <!-- <template #="{ row, $index }">
            <div class="number"><span>{{ row.lengthFt }} | {{ row.lengthM }}</span></div>
        </template> -->
      </el-table-column>
      <el-table-column :label="$t('boat.width')" show-overflow-tooltip prop="maxBeam" width="170px">
        <!-- <template #="{ row, $index }">
            <div class="number"><span>{{ row.maxBeamFt }} | {{ row.maxBeamM }}</span></div>
        </template> -->
      </el-table-column>
      <el-table-column :label="$t('boat.speed')" show-overflow-tooltip prop="maxSpeedKn" width="200px">
        <template #="{ row, $index }">
            <div class="number"><span>{{ row.maxSpeedKn }} / {{ row.maxSpeedMph }}</span></div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('boat.weight')" show-overflow-tooltip prop="maxWeightLb" width="170px">
        <template #="{ row, $index }">
            <div class="number"><span>{{ row.maxWeightLb }} / {{ row.maxWeightTon }}</span></div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('boat.feature')" show-overflow-tooltip prop="feature" width="100px"></el-table-column>
      <el-table-column :label="$t('boat.description')" resizable show-overflow-tooltip prop="depict" width="270px"></el-table-column>
      <!-- <el-table-column label="图片" width="150px">
                <template #="{ row, $index }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;">
                </template>
            </el-table-column> -->
      <el-table-column :label="$t('operation.oper')" width="150px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            v-auth="auths.edit"
            @click="add(row)"
          ></el-button>
          <el-popconfirm
            :title="$t('operation.ask')"
            width="200px"
            :confirm-button-text="$t('operation.confirm')"
            :cancel-button-text="$t('operation.cancel')"
            @confirm="removeRow(row.id)"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                v-auth="auths.del"
                icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getPage"
      @size-change="handler"
    />

    <Save ref="save" @callback="getPage()" />
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
//引入请求
import { vesselsGetPage, vesselsDelete } from '@/api/boat/basic'
//引入ts类型
import { ElMessage } from 'element-plus'
import Save from './save.vue'
import { brandGetAll } from '@/api/boat/brand'
import { typeGetAll } from '@/api/boat/type'

const auths = {
  add: '/t-vessels-info/add',
  edit: '/t-vessels-info/update',
  del: '/t-vessels-info/delete',
}
const save = ref()
//分页器当前页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(10)
let total = ref<number>(0)
let list = ref([])
//控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
//组件挂载完毕
onMounted(() => {
  getPage()
  getBrands()
  getTypes()
})

let brands = ref([])
const getBrands = async () => {
  let { status, data } = await brandGetAll()

  if (status == 200) {
    brands.value = data
  }
}

let types = ref([])
const getTypes = async () => {
  let { status, data } = await typeGetAll()

  const resolve = (list) => {
    list.forEach(element => {
      if (element.children && element.children.length) {
        element.disabled = true
      }
    })
  }
  resolve(data)

  if (status == 200) {
    types.value = data
  }
}

const query = reactive({
  typeId: '',
  brandId: '',
  model: '',
})

const search = () => {
  getPage(1)
}
const getPage = async (pager = 1) => {
  //当前分页器的页码
  pageNo.value = pager
  let { status, data, msg } = await vesselsGetPage({
    pageNo: pageNo.value,
    pageSize: pageSize.value,
    ...query
  })
  if (status == 200) {
    let { records, total: tatalNums } = data
    total.value = tatalNums
    list.value = records
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}
//分页器下拉菜单发生变化触发
const handler = (pageSizes: number) => {
  getPage()
}

const add = (row: any) => {
  save.value.show(row)
}

const removeRow = async (id: number) => {
  //删除某一个已有商品的情况
  let result: any = await vesselsDelete(id)
  if (result.status == 200) {
    //提示信息
    ElMessage({ type: 'success', message: '删除成功' })
    //获取已有全部商品
    getPage()
  } else {
    //删除失败
    ElMessage({ type: 'error', message: '系统数据不能删除' })
  }
}
</script>

<style scoped>
.number {
  text-align: center;
}

.search {
  width: 100%;
  display: flex;
  align-items: center;
}
.inpt {
  width: 120px;
  margin-right: 10px;
}

.btn {
  margin-right: 10px;
}
</style>
